﻿using System.IO;
using System.IO.Compression;

namespace 修为尽失.辅助.exts
{
    public static class IO
    {
        public static void Append(this Stream stream, Stream descSream)
        {
            var buffer = new byte[descSream.Length];
            descSream.Read(buffer, 0, buffer.Length);
            descSream.Seek(0L, SeekOrigin.Begin);
            stream.Write(buffer, 0, buffer.Length);
        }

        public static byte[] Compress(this byte[] bytes)
        {
            using (MemoryStream compressStream = new MemoryStream())
            {
                using (var zipStream = new GZipStream(compressStream, CompressionMode.Compress)) zipStream.Write(bytes, 0, bytes.Length);
                return compressStream.ToArray();
            }
        }

        public static byte[] Decompress(this byte[] bytes)
        {
            using (var compressStream = new MemoryStream(bytes))
            {
                using (var zipStream = new GZipStream(compressStream, CompressionMode.Decompress))
                {
                    using (var resultStream = new MemoryStream())
                    {
                        zipStream.CopyTo(resultStream);
                        return resultStream.ToArray();
                    }
                }
            }
        }
    }
}